package com.bw.video_module.video;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.viewbinding.ViewBinding;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.AppUtils;
import com.bw.adapter_sdk.quick.BaseQuickMoreAdapter;
import com.bw.common.entity.LiveDataEntity;
import com.bw.common.mvvm.view.BaseFragment;
import com.bw.network_sdk.videoentity.VideoEntity;
import com.bw.network_sdk.videoentity.VideoValuesEntity;
import com.bw.video_module.databinding.FragmentVideoBinding;
import com.bw.video_module.video.adapter.VideoAdapter;

import java.io.Serializable;

import constant.UiType;
import model.UiConfig;
import update.UpdateAppUtils;

public class VideoFragment extends BaseFragment<FragmentVideoBinding, VideoViewModel> implements BaseQuickMoreAdapter.OnItemClickListener {

    private VideoAdapter videoAdapter;

    @Override
    public View bindLayout() {
        return v.getRoot();
    }

    @Override
    public ViewBinding initBinding() {
        return FragmentVideoBinding.inflate(inflater);
    }

    @Override
    public void initView() {

        if (AppUtils.getAppVersionCode() < 2) {
            UiConfig uiConfig = new UiConfig();
            uiConfig.setUiType(UiType.PLENTIFUL);
            UpdateAppUtils.getInstance()
                    .apkUrl("http://118.195.161.134:8077/fileDownload?fileName=qq.apk")
                    .updateTitle("当前版本可更新到" + AppUtils.getAppVersionName())
                    .uiConfig(uiConfig)
                    .updateContent("新版修复首页ui挤压bug")
                    .update();
        }

        v.videoRecycler.setLayoutManager(new LinearLayoutManager(getContext()));
        videoAdapter = new VideoAdapter();
        videoAdapter.setOnItemClickListener(this);
    }

    @Override
    public void initData() {
        v.videoRecycler.setAdapter(videoAdapter);
    }

    @Override
    public void handleMessage(LiveDataEntity entity) {
        switch (entity.action) {
            case START_VIEW_MODEL:
                VideoEntity videoEntity = (VideoEntity) entity.data.get(LiveDataEntity.LiveDataType.ENTITY);
                Log.i("aaa", "获取到数据" + videoEntity.data.toString());
                videoAdapter.setNewInstance(videoEntity.data.data);
                break;
        }
    }

    @Override
    public void onItemClick(BaseQuickMoreAdapter adapter, int position) {
        if (adapter instanceof VideoAdapter) {
            VideoAdapter videoAdapter = (VideoAdapter) adapter;
            VideoValuesEntity videoValuesEntity = videoAdapter.getData().get(position);
            Bundle bundle = new Bundle();
            bundle.putSerializable("videoEntity", (Serializable) videoValuesEntity);
            Log.i("ggg", videoValuesEntity.toString());
            ARouter.getInstance().build("/sofa/videodetail")
                    .withBundle("bundle", bundle)
                    .navigation(getContext());
        }
    }
}